import request from '@/utils/request'
import type { PermissonListResponseData, RoleInfoResponseData } from './type'

const API = {
  ROLEINFOBYPAGE_URL: '/admin/acl/role/',
  ADDROLE_URL: '/admin/acl/role/save',
  UPDATEROLE_URL: '/admin/acl/role/update',
  PERMISSIONLIST_URL: '/admin/acl/permission/toAssign/',
  SETPERMISSION_URL: '/admin/acl/permission/doAssign',
  DELETEROLE_URL: '/admin/acl/role/remove/',
}

export const reqRoleInfoByPage = async (
  page: number,
  limit: number,
  roleName: string,
) =>
  await request.get<any, RoleInfoResponseData>(
    API.ROLEINFOBYPAGE_URL + `${page}/${limit}?roleName=${roleName}`,
  )

export const reqAddOrUpdateRole = async (data: {
  roleName: string
  id?: number
}) => {
  if (!data.id) {
    return await request.post<any, any>(API.ADDROLE_URL, data)
  } else {
    //根据接口数据格式做处理
    let _data = {
      roleName: data.roleName,
      role_id: data.id,
    }
    return await request.put<any, any>(API.UPDATEROLE_URL, _data)
  }
}

export const reqPermissionList = async (roleId: number) =>
  await request.get<any, PermissonListResponseData>(
    API.PERMISSIONLIST_URL + roleId,
  )

export const reqSetPermission = async (roleId: number, idArr: number[]) => {
  let idString = idArr.join(',')
  return await request.post<any, any>(
    API.SETPERMISSION_URL + `?roleId=${roleId}&permissionId=${idString}`,
  )
}

export const reqDeleteRole = async (roleId: number) =>
  await request.delete<any, any>(API.DELETEROLE_URL + roleId)
